/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sgs.mb;

import br.com.sgs.auxiliar.StatusAux;
import br.com.sgs.bundle.MessageBundleLoader;
import br.com.sgs.entity.Status;
import br.com.sgs.fachada.Fachada;
import br.com.sgs.fachada.IFachada;
import br.com.sgs.fachada.StatusFachada;
import br.com.sgs.fachada.StatusIFachada;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

        
             
        
                
/**
 *
 * @author Eric
 */
@ManagedBean(name="statusMB")
@ViewScoped
public class StatusMB implements Serializable {
    
    
    private List<Status> lsstatus = new ArrayList<Status>();
    
    private Status statusselecionado;
       
    //parametro de busca
    private String busca;
    
    
    /**
     * Creates a new instance of InstituicaoNew
     */
    public StatusMB() {
    }
    
    @PostConstruct
    public void init() {
        try {

            StatusIFachada ifach = new StatusFachada();
            setLsstatus(ifach.findStatus());

        } catch (Exception e) {
            System.err.println("Erro no init do statusMB");
        }
    }
   
    public void atualizarTabela() {
        try {

            StatusIFachada ifach = new StatusFachada();

            setLsstatus(ifach.findStatus());


        } catch (Exception e) {
            System.err.println("Erro no atualizarTabela");
        }
    }

    
    public void buscar() {
        try {
            
            StatusIFachada ifach = new StatusFachada();
            setLsstatus(ifach.findStatus());

            
        } catch (Exception e) {
            System.err.println("Erron no método buscar");
            e.printStackTrace();
        }
    }
    
    public void excluir() {
        try {

            IFachada ifach = new Fachada();

            Status status = getStatusselecionado();

            status.setStatus(StatusAux.EXCLUIDO);
            ifach.alterar(status);

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, MessageBundleLoader.getMessage("msgExcluidoSucesso"), ""));
            atualizarTabela();

        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, MessageBundleLoader.getMessage("msgexclusaoErro"), ""));
        }

    }

    public String editar() {
        try {
           return "STATUSEDIT";
        } catch (Exception e) {
           return null;
        }
    }
    
    
    public void selecionarRegistro(Status status) {
        try {

            setStatusselecionado(status);

        } catch (Exception e) {
            System.err.println("Erro no metodo selecionarRegistro");
        }

    }
    
    public String strStatusLabel(int st) {
        return MessageBundleLoader.getMessage(StatusAux.getStatusLabel(st));
    }

    /**
     * @return the lsstatus
     */
    public List<Status> getLsstatus() {
        return lsstatus;
    }

    /**
     * @param lsstatus the lsstatus to set
     */
    public void setLsstatus(List<Status> lsstatus) {
        this.lsstatus = lsstatus;
    }

    /**
     * @return the statusselecionado
     */
    public Status getStatusselecionado() {
        return statusselecionado;
    }

    /**
     * @param statusselecionado the statusselecionado to set
     */
    public void setStatusselecionado(Status statusselecionado) {
        this.statusselecionado = statusselecionado;
    }

    /**
     * @return the busca
     */
    public String getBusca() {
        return busca;
    }

    /**
     * @param busca the busca to set
     */
    public void setBusca(String busca) {
        this.busca = busca;
    }

  
}
